/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.collect.ImmutableMap;
/*     */ import com.google.common.collect.ImmutableMap.Builder;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.text.Collator;
/*     */ import java.util.Comparator;
/*     */ import java.util.Locale;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public enum SortBy
/*     */ {
/*  36 */   NONE("none", Key.NONE, Direction.ASC), 
/*  37 */   DATE_ASC("dateAsc", Key.DATE, Direction.ASC), 
/*  38 */   DATE_DESC("dateDesc", Key.DATE, Direction.DESC), 
/*  39 */   SUBJ_ASC("subjAsc", Key.SUBJECT, Direction.ASC), 
/*  40 */   SUBJ_DESC("subjDesc", Key.SUBJECT, Direction.DESC), 
/*  41 */   NAME_ASC("nameAsc", Key.SENDER, Direction.ASC), 
/*  42 */   NAME_DESC("nameDesc", Key.SENDER, Direction.DESC), 
/*  43 */   RCPT_ASC("rcptAsc", Key.RCPT, Direction.ASC), 
/*  44 */   RCPT_DESC("rcptDesc", Key.RCPT, Direction.DESC), 
/*  45 */   SIZE_ASC("sizeAsc", Key.SIZE, Direction.ASC), 
/*  46 */   SIZE_DESC("sizeDesc", Key.SIZE, Direction.DESC), 
/*  47 */   ATTACHMENT_ASC("attachAsc", Key.ATTACHMENT, Direction.ASC), 
/*  48 */   ATTACHMENT_DESC("attachDesc", Key.ATTACHMENT, Direction.DESC), 
/*  49 */   FLAG_ASC("flagAsc", Key.FLAG, Direction.ASC), 
/*  50 */   FLAG_DESC("flagDesc", Key.FLAG, Direction.DESC), 
/*  51 */   PRIORITY_ASC("priorityAsc", Key.PRIORITY, Direction.ASC), 
/*  52 */   PRIORITY_DESC("priorityDesc", Key.PRIORITY, Direction.DESC), 
/*     */   
/*     */ 
/*  55 */   NAME_NATURAL_ORDER_ASC(null, Key.NAME_NATURAL_ORDER, Direction.ASC), 
/*  56 */   NAME_NATURAL_ORDER_DESC(null, Key.NAME_NATURAL_ORDER, Direction.DESC), 
/*     */   
/*  58 */   NAME_LOCALIZED_ASC(null, Key.NAME, Direction.ASC), 
/*  59 */   NAME_LOCALIZED_DESC(null, Key.NAME, Direction.DESC), 
/*     */   
/*     */ 
/*  62 */   TASK_DUE_ASC("taskDueAsc", Key.DATE, Direction.ASC), 
/*  63 */   TASK_DUE_DESC("taskDueDesc", Key.DATE, Direction.DESC), 
/*  64 */   TASK_STATUS_ASC("taskStatusAsc", Key.DATE, Direction.ASC), 
/*  65 */   TASK_STATUS_DESC("taskStatusDesc", Key.DATE, Direction.DESC), 
/*  66 */   TASK_PERCENT_COMPLETE_ASC("taskPercCompletedAsc", Key.DATE, Direction.ASC), 
/*  67 */   TASK_PERCENT_COMPLETE_DESC("taskPercCompletedDesc", Key.DATE, Direction.DESC);
/*     */   
/*     */   public static enum Key {
/*  70 */     DATE,  SENDER,  RCPT,  SUBJECT,  ID,  NONE,  NAME,  NAME_NATURAL_ORDER,  SIZE,  ATTACHMENT,  FLAG,  PRIORITY;
/*     */     
/*     */     private Key() {} }
/*     */   
/*  74 */   public static enum Direction { DESC,  ASC;
/*     */     
/*     */     private Direction() {} }
/*     */   
/*     */   private static final Map<String, SortBy> NAME2SORT;
/*  79 */   static { ImmutableMap.Builder<String, SortBy> builder = ImmutableMap.builder();
/*  80 */     for (SortBy sort : values()) {
/*  81 */       if (sort.name != null) {
/*  82 */         builder.put(sort.name.toLowerCase(), sort);
/*     */       }
/*     */     }
/*  85 */     NAME2SORT = builder.build();
/*     */   }
/*     */   
/*     */   private final String name;
/*     */   private final Key key;
/*     */   private final Direction direction;
/*     */   private SortBy(String name, Key key, Direction dir)
/*     */   {
/*  93 */     this.name = name;
/*  94 */     this.key = key;
/*  95 */     this.direction = dir;
/*     */   }
/*     */   
/*     */   public static SortBy of(String key) {
/*  99 */     return key != null ? (SortBy)NAME2SORT.get(key.toLowerCase()) : null;
/*     */   }
/*     */   
/*     */   public Key getKey() {
/* 103 */     return this.key;
/*     */   }
/*     */   
/*     */   public Direction getDirection() {
/* 107 */     return this.direction;
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 112 */     return this.name != null ? this.name : name();
/*     */   }
/*     */   
/*     */   public Comparator<ZimbraHit> getHitComparator(Locale locale) {
/* 116 */     locale = locale != null ? locale : Locale.getDefault();
/* 117 */     return new NameComparator(this, Collator.getInstance(locale));
/*     */   }
/*     */   
/*     */   private static final class NameComparator implements Comparator<ZimbraHit> {
/*     */     private final SortBy sort;
/*     */     private final Collator collator;
/*     */     
/*     */     NameComparator(SortBy sort, Collator collator) {
/* 125 */       this.sort = sort;
/* 126 */       this.collator = collator;
/*     */     }
/*     */     
/*     */     public int compare(ZimbraHit lhs, ZimbraHit rhs)
/*     */     {
/* 131 */       int result = 0;
/*     */       try {
/* 133 */         result = this.collator.compare(lhs.getName(), rhs.getName());
/* 134 */         if (result == 0) {
/* 135 */           result = lhs.getItemId() - rhs.getItemId();
/*     */         }
/* 137 */         if (this.sort.getDirection() == SortBy.Direction.DESC) {
/* 138 */           result = -1 * result;
/*     */         }
/*     */       } catch (ServiceException e) {
/* 141 */         ZimbraLog.index.debug("Failed to compare %s and %s", lhs, rhs, e);
/* 142 */         result = 0;
/*     */       }
/* 144 */       return result;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/SortBy.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */